	use "$data\asi2005_inputlevel04082013.dta", clear
	drop District
	gen survey="ASI"
	append using "$data\sum2005_inputlevel04082013.dta"
	replace state=State if missing(state)
	gen input_nec = strmatch( desc,"*n.e.c*")
	gen input_other = strmatch( desc,"*other*")
	gen input_nec_other = max( input_nec,input_other)
	tab survey input_nec_other
	drop if input_nec_other==1
	
	drop if ln_price==.|ln_L==.

	gen counter = 1
	sort survey input_unit_cat ln_price
	by survey input_unit_cat: gen input_counter = sum(counter)
	egen input_no_obs = max(input_counter), by (survey input_unit_cat)



	areg ln_price ln_L if survey=="ASI", absorb(input_unit_cat) vce(cluster input_unit_cat)
	outreg2 using "$results\input_12split1_4split2", replace addst(Cluster, e(N_clust), FE, e(df_a)) excel stats(coef se pval) bdec(3) sdec(4) ctitle("ASI, No Corr")
	
	merge m:1 input_unit_cat using "$data\inputs units problem\summary.dta"
	tab survey if _merge==1
	*** inputs with very few ASI observation not matched. Dropped.
	tab input_no_obs if _merge~=3&survey=="ASI"
	drop if _merge==2
	
			drop if input_no_obs<10&survey=="ASI"


		** Tabbing problemscale and nssinwhichcategory for different problems. These whould show up only for problem==1,2
		tab problemscale nssin if problem==0
		tab problemscale nssin if problem==1
		tab problemscale nssin if problem==2
		tab problemscale nssin if problem==3
		tab problemscale nssin if problem==4
		tab problemscale nssin if problem==5
		tab problemscale nssin if problem==6
		tab problemscale nssin if problem==7
		tab problemscale nssin if problem==8
		* there is 1 asicc with problem==1 and nss==4 which is weird. The NSS was a misreporting I think. Drop it for now - just 2 observations
		drop if problem==1&nssin=="4"&survey=="SUM"
		
		
		gen input_unit_cat_old = input_unit_cat

			replace input_unit_cat = input_unit_cat*10 + 1 if ln_price>cutoffprice&(problem==1|problem==2)
			replace input_unit_cat = input_unit_cat*10 + 2 if ln_price<cutoffprice&(problem==1|problem==2)
// // 		}

			drop if survey=="SUM"& nssinwhichcategory=="weird"
			drop if survey=="SUM"& nssinwhichcategory=="4" /* this was just one asicc which was weird in NSS */


		* Winsorize L within survey
		drop counter input_counter input_no_obs
// 		egen tag = tag(survey fid)
		gen ln_L_winsor = ln_L
		su ln_L if survey=="ASI", de
		replace ln_L_winsor = `r(p1)' if ln_L<=`r(p1)'&survey=="ASI"
		replace ln_L_winsor = `r(p99)' if ln_L>=`r(p99)'&survey=="ASI"
		su ln_L_winsor if survey=="ASI", de
		su ln_L if survey=="SUM", de
		replace ln_L_winsor = `r(p1)' if ln_L<=`r(p1)'&survey=="SUM"
		replace ln_L_winsor = `r(p99)' if ln_L>=`r(p99)'&survey=="SUM"
		su ln_L_winsor if survey=="SUM", de

		* Winsorize prices within survey,input,unit categories
		gen counter = 1
		sort survey input_unit_cat ln_price
		by survey input_unit_cat: gen input_counter = sum(counter)
		egen input_no_obs = max(input_counter), by (survey input_unit_cat)
		foreach per_drop in 1 5 10 {
			gen obs_winsor_low_bound`per_drop' = floor(input_no_obs*`per_drop'/100) + 1 
			gen obs_winsor_upper_bound`per_drop' =  input_no_obs -  obs_winsor_low_bound`per_drop'
			gen ln_price_lowbound_temp`per_drop' = ln_price if obs_winsor_low_bound`per_drop'==input_counter
			egen ln_price_lowbound`per_drop' = min(ln_price_lowbound_temp`per_drop') if obs_winsor_low_bound`per_drop'>=input_counter,by(survey input_unit_cat)
			gen ln_price_upbound_temp`per_drop' = ln_price if obs_winsor_upper_bound`per_drop'==input_counter
			egen ln_price_upbound`per_drop' = min(ln_price_upbound_temp`per_drop') if obs_winsor_upper_bound`per_drop'<=input_counter,by(survey input_unit_cat)
			gen ln_price_winsor`per_drop' = ln_price
			replace ln_price_winsor`per_drop' = ln_price_lowbound`per_drop' if ln_price_lowbound`per_drop'~=.
			replace ln_price_winsor`per_drop' = ln_price_upbound`per_drop' if ln_price_upbound`per_drop'~=.
		}
		gen rural=Sector
		label variable ln_price_winsor1 "Log(input price)"
		label variable ln_L_winsor "Log(labor)"
		eststo clear
		eststo: reghdfe ln_price_winsor1 ln_L_winsor if survey=="ASI", absorb(input_unit_cat#state#rural) vce(cluster input_unit_cat_old)
		estadd scalar Observations=e(N_full)
		estadd scalar Single=e(num_singletons) 
		estadd local Fixed1 "Yes"
// 		estadd local Products "1218"
		estadd local PR50_5=round(exp(e(b)[1,1]*ln(50))/exp(e(b)[1,1]*ln(5)),0.01)
		estadd scalar PR500_5=round(exp(e(b)[1,1]*ln(500))/exp(e(b)[1,1]*ln(5)),0.01)
		estadd local Fixed4 "Yes"
		estadd local Winsor "Yes"
		estadd local Cluster "Product"
		estadd local Sample "ASI"
		estadd local NumClust=e(N_clust)
		eststo: reghdfe ln_price_winsor1 ln_L_winsor if survey=="SUM", absorb(input_unit_cat#state#rural) vce(cluster input_unit_cat_old)
		estadd scalar Observations=e(N_full)	
		estadd local Fixed1 "Yes"
// 		estadd local Products "2740"
		estadd local Sample "SUM"
		estadd local NumClust=e(N_clust)
		estadd local Cluster "Product"
		estadd local Winsor "Yes"
		estadd local PR50_5=round(exp(e(b)[1,1]*ln(50))/exp(e(b)[1,1]*ln(5)),0.01)
		estadd local PR500_5=round(exp(e(b)[1,1]*ln(500))/exp(e(b)[1,1]*ln(5)),0.01)
eststo: reghdfe ln_price_winsor1 ln_L_winsor, absorb(input_unit_cat#state#rural) vce(cluster input_unit_cat_old)
		estadd scalar Observations=e(N_full)	
		estadd local Fixed1 "Yes"
		estadd local Sample "Both"
// 		estadd local Products "3182"
		estadd local NumClust=e(N_clust)
		estadd local Cluster "Product"
		estadd local Winsor "Yes"
		estadd local PR50_5=round(exp(e(b)[1,1]*ln(50))/exp(e(b)[1,1]*ln(5)),0.01)
		estadd local PR500_5=round(exp(e(b)[1,1]*ln(500))/exp(e(b)[1,1]*ln(5)),0.01)
esttab, label
		esttab, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("PR50_5 Price Ratio (Size 50 to 5)" "PR500_5 Price Ratio (Size 500 to 5)" "j \hline" "Sample Sample"  "Winsor Winsor" Observations "k \hline "   "Fixed1 State x Rural x Product FE" "Products Number of Products" "Cluster SE clusters:"   "NumClust Number of Clusters" ) 
	esttab using $results\Old_Table_6.tex, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("PR50_5 Price Ratio (Size 50 to 5)" "PR500_5 Price Ratio (Size 500 to 5)" "j \hline" "Sample Sample"  "Winsor Winsor" Observations "k \hline "   "Fixed1 State x Rural x Product FE" "Products Number of Products" "Cluster SE clusters:"   "NumClust Number of Clusters" ) 
		esttab using $results\T6_Table_6b.tex, noobs se b(a2) ar2 star(* 0.10 ** 0.05 *** 0.01) nocon label legend replace scalars("PR50_5 Price Ratio (Size 50 to 5)" "PR500_5 Price Ratio (Size 500 to 5)" "j \hline" "Sample Sample"  "Winsor Winsor" Observations "k \hline "   "Fixed1 State x Rural x Product FE" "NumClust Number of Clusters" "Cluster SE clusters:"   ) 